// pages/goods/list/index.js
import { getGoodsList } from '../../../api/goods'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    goodsList: [], // 商品列表数据
    total: 0,
    isFinish: false, // 判断数据是否加载完毕
    request: false,
    requestData: {
      page: 1,
      limit: 10,
      category1Id: '',
      category2Id: ''
    }
  },
  onLoad(options){
    Object.assign(this.data.requestData,options)
  },
  onShow(){
    this.getgoodData()
  },
  async getgoodData(){
    this.data.request = true
    const { data } = await getGoodsList(this.data.requestData)
    this.data.request = false
    this.setData({
      goodsList: [ ...this.data.goodsList, ...data.records ],
      total: data.total
    })
  },
  onReachBottom(){
    const { total, goodsList, requestData, request }  = this.data
    const { page } = requestData
    if(request) return
    if(total === goodsList.length) {
      this.setData({
        isFinish: true
      })
      return
    }
    this.setData({
      requestData: { ...this.data.requestData, page: page + 1 }
    })
    this.getgoodData()
  },
  onPullDownRefresh(){
    this.setData({
      goodsList: [],
      total: 0,
      isFinish: false,
      requestData: {...this.data.requestData, page: 1}
    })
    this.getgoodData()
    wx.stopPullDownRefresh()
  },
  onShareAppMessage(){
    return {
      title: this.data.goodsList[0].floralLanguage,
      path: "/modules/goodModule/pages/goods/list/list",
      imageUrl: this.data.goodsList[0].imageUrl
    }
  },
  onShareTimeline(){
    return {
      title: this.data.goodsList[0].floralLanguage,
      imageUrl: this.data.goodsList[0].imageUrl
    }
  }
})
